class Main extends eui.UILayer {
    /**
     * 加载进度界面
     * loading process interface
     */
    private theme: eui.Theme;
    private isResourceLoadEnd: boolean = false;
    private isThemeLoadEnd: boolean = false;
    private loadingView: LoadingUI;
    private screenContainer:egret.DisplayObjectContainer;
    // 音乐控件容器
    private musicContainer:egret.DisplayObjectContainer;
    public musicIcon: MusicIcon;

    protected createChildren(): void {
        super.createChildren();
        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
        egret.ImageLoader.crossOrigin = "anonymous";
        // utils.CoreData.instance.userid=egret.getOption("userid");
        // utils.CoreData.instance.userid="d040c946f889f3118f55f812427f3f03";
        this.getInitViewAndLoadConfig();
    }
    // 初始化缩放视图并开始加载资源
    private getInitViewAndLoadConfig():void
    {
        DisUtil.size(this, 640, 1138);
        DisUtil.get(this).fitW();
        utils.CoreData.instance.stageW = this.stage.stageWidth;
        utils.CoreData.instance.stageH = this.stage.stageHeight;
        utils.CoreData.instance.stage=this.stage;
        StageUtils.stage = this.stage;
        let stageScale=DisUtil.screenSale;
        utils.CoreData.instance.stageScale = stageScale;
        utils.CoreData.instance.screenW = document.documentElement.clientWidth * stageScale;
        utils.CoreData.instance.screenH = document.documentElement.clientHeight * stageScale;
        // initialize the Resource loading library
        //初始化Resource资源加载库
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");
    }
    /**
     * 配置文件加载完成,开始预加载皮肤主题资源和preload资源组。
     * Loading of configuration file is complete, start to pre-load the theme configuration file and the preload resource group
     */
    private onConfigComplete(event: RES.ResourceEvent): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onLoadResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup("load");
    }

    /**
     * preload资源组加载完成
     * Preload resource group is loaded
     */
    private onLoadResourceLoadComplete(event: RES.ResourceEvent): void {
        if (event.groupName == "load") {
            //设置加载进度界面
            utils.CoreData.instance.stageW = this.stage.stageWidth;
            utils.CoreData.instance.stageH = this.stage.stageHeight;
            this.loadingView = new LoadingUI();
            this.addChild(this.loadingView);
            //this.validateNow();

            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。       
            this.theme = new eui.Theme("resource/default.thm.json", this.stage);
            this.theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
            RES.loadGroup("preload");
        }
        else if (event.groupName == "preload") {
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onLoadResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            this.isResourceLoadEnd = true;
            this.requestLogin();
        }
    }


    /**
     * 主题文件加载完成,开始预加载
     * Loading of theme configuration file is complete, start to pre-load the 
     */
    private onThemeLoadComplete(): void {
        this.theme.removeEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
        this.theme = null;
        this.isThemeLoadEnd = true;
        this.requestLogin();
    }
    //请求后端的登录接口
    private requestLogin():void
    {
        if (this.isThemeLoadEnd == false) return;
        if (this.isResourceLoadEnd == false) return;
        // pub.SignalManager.instance.addSignal(pub.SignalDefin.LOAD_SUCCESS,this.loginSuccessHandle,this);
        // utils.ServerManager.instance.requestLogin();

        // this.loginSuccessHandle();
        this.createScene();
    }

    private loginSuccessHandle():void
    {
        pub.SignalManager.instance.removeSignal(pub.SignalDefin.LOAD_SUCCESS,this.createScene,this)
        this.createScene();
    }

    private createScene() {
        this.loadingView.ClearMv();
        this.removeChild(this.loadingView);
        utils.Toast.instance.setStageContainer(this.stage);
        this.startCreateScene();
    }
    /**
     * 创建场景界面
     * Create scene interface
     */
    protected startCreateScene(): void {
        this.screenContainer=new egret.DisplayObjectContainer();
        this.addChild(this.screenContainer);
        var mainScreen: BaseScreen = new MainScreen();
        this.screenContainer.addChild(mainScreen);
        this.getMusicIcon();
        utils.ScreenManager.instance.baseContainer = this.screenContainer;
        utils.ScreenManager.instance.curScreen = mainScreen;
        utils.ScreenManager.instance.MainContainer = this;
        utils.ScreenManager.instance.main_screen = <MainScreen>mainScreen;
    }

    public getMusicIcon(): void {
        this.musicContainer=new egret.DisplayObjectContainer();
        this.addChild(this.musicContainer);
        this.musicIcon = new MusicIcon();
        // this.musicIcon.scaleX = 0.9;
        // this.musicIcon.scaleY = 0.9;
        this.musicContainer.x = 640-30;
        this.musicContainer.y = 30 ;
        this.musicContainer.addChild(this.musicIcon);
    }

    public share_click():void{
        console.log('m_ddd');
    }

    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onItemLoadError(event: RES.ResourceEvent): void {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }
    /**
     * 资源组加载出错
     * Resource group loading failed
     */
    private onResourceLoadError(event: RES.ResourceEvent): void {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //ignore loading failed projects
    }
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
    private onResourceProgress(event: RES.ResourceEvent): void {
        if (event.groupName == "preload") {
            this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }

    
}
